package com.xxxx.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import com.xxxx.yygh.enums.PaymentTypeEnum;
import com.xxxx.yygh.enums.RefundStatusEnum;
import com.xxxx.yygh.model.order.OrderInfo;
import com.xxxx.yygh.model.order.PaymentInfo;
import com.xxxx.yygh.model.order.RefundInfo;
import com.xxxx.yygh.order.service.OrderService;
import com.xxxx.yygh.order.service.PaymentService;
import com.xxxx.yygh.order.service.RefundInfoService;
import com.xxxx.yygh.order.service.WeixinService;
import com.xxxx.yygh.order.utils.ConstantPropertiesUtils;
import com.xxxx.yygh.order.utils.HttpClient;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class WeixinServiceImpl implements WeixinService {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RefundInfoService refundInfoService;


    /**
     * 根据orderId生成微信二维码
     */
    @Override
    public Map createNative(Long orderId) {
        try {
            //从redis获取数据 如果还未过期就能拿到
            Map payMap = (Map) redisTemplate.opsForValue().get(orderId.toString());
            if (payMap != null) {
                return payMap;
            }

            //1. 根据orderId获取订单信息
            OrderInfo orderInfo = orderService.getById(orderId);
            //2. 向支付记录表里添加信息  第二个参数为支付类型
            paymentService.savePaymentInfo(orderInfo, PaymentTypeEnum.WEIXIN.getStatus());

            //3. 设置参数，调用微信生成二维码接口
            //把参数转换成xml格式，使用商户key进行加密
            Map paramMap = new HashMap<>();
            paramMap.put("appid", ConstantPropertiesUtils.APPID);
            paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            String body = orderInfo.getReserveDate() + "就诊" + orderInfo.getDepname();
            paramMap.put("body", body);
            paramMap.put("out_trade_no", orderInfo.getOutTradeNo());
            //paramMap.put("total_fee", orderInfo.getAmount().multiply(new BigDecimal("100")).longValue()+"");
            paramMap.put("total_fee", "1"); //测试金额 0.01
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("notify_url", "http://guli.shop/api/order/weixinPay/weixinNotify");
            paramMap.put("trade_type", "NATIVE");  //支付类型为扫描

            //2、HTTPClient来根据URL访问第三方接口并且传递参数
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            //设置map参数
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
            client.setHttps(true);
            client.post();

            // 返回相关信息
            String xml = client.getContent();
            // 转换成map
            Map<String,String> resultMap = WXPayUtil.xmlToMap(xml);

            // 封装并返回结果
            Map map = new HashMap();
            map.put("orderId", orderId);
            map.put("totalFee", orderInfo.getAmount()); //实付0.01 返回100 哈哈
            map.put("resultCode", resultMap.get("result_code"));
            map.put("codeUrl", resultMap.get("code_url")); //二维码地址

            // 存入redis 不存这个map的话 可能跟第一次返回的数据不一样 因为如果有的话是直接返回
            if (map.get("result_code") != null) {
                redisTemplate.opsForValue().set(orderId.toString(), map,15, TimeUnit.MINUTES);
            }
            return map;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据订单id 查询订单状态 由前端定时请求
     */
    @Override
    public Map<String, String> queryPayStatus(Long orderId) {
        try{
            //1.根据orderId获取订单信息
            OrderInfo orderInfo = orderService.getById(orderId);

            //2.封装请求提交参数
            Map paramMap = new HashMap<>();
            paramMap.put("appid", ConstantPropertiesUtils.APPID);  //微信支付id
            paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);    //商户号
            paramMap.put("out_trade_no", orderInfo.getOutTradeNo());    //订单编号
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());    //随机字符串

            //3.设置请求内容(去微信提供的地址请求)
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
            client.setHttps(true);
            client.post();

            //4.得到微信接口返回数据 看看支付成功没有
            String xml = client.getContent();
            Map<String,String> resultMap = WXPayUtil.xmlToMap(xml);
            return resultMap;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean refund(Long orderId) {
        try {
            //获取支付记录信息
            PaymentInfo paymentInfo = paymentService.getPaymentInfo(orderId, PaymentTypeEnum.WEIXIN.getStatus());
            //添加信息到退款记录表
            RefundInfo refundInfo = refundInfoService.saveRefundInfo(paymentInfo);
            //判断当前订单数据是否已经退款
            if(refundInfo.getRefundStatus().intValue() == RefundStatusEnum.REFUND.getStatus().intValue()) {
                return true;
            }
            //还未退款，调用微信接口实现退款
            //封装需要参数
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("appid",ConstantPropertiesUtils.APPID);       //公众账号ID
            paramMap.put("mch_id",ConstantPropertiesUtils.PARTNER);   //商户编号
            paramMap.put("nonce_str",WXPayUtil.generateNonceStr());
            paramMap.put("transaction_id",paymentInfo.getTradeNo()); //微信订单号
            paramMap.put("out_trade_no",paymentInfo.getOutTradeNo()); //商户订单编号
            paramMap.put("out_refund_no","tk"+paymentInfo.getOutTradeNo()); //商户退款单号

            //实际开发的金额应该根据订单而确定，这里写死1分钱(但是最大退款金额不得超过支付金额)
            //paramMap.put("total_fee",paymentInfoQuery.getTotalAmount().multiply(new BigDecimal("100")).longValue()+"");
            //paramMap.put("refund_fee",paymentInfoQuery.getTotalAmount().multiply(new BigDecimal("100")).longValue()+"");
            paramMap.put("total_fee","1");
            paramMap.put("refund_fee","1");

            String paramXml = WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY);
            //设置调用退款接口内容
            HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/secapi/pay/refund");
            client.setHttps(true);
            client.setXmlParam(paramXml);
            //设置证书信息
            client.setCert(true);
            client.setCertPassword(ConstantPropertiesUtils.PARTNER);
            client.post();

            //接收返回数据
            String xml = client.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
            if (null != resultMap && WXPayConstants.SUCCESS.equalsIgnoreCase(resultMap.get("result_code"))) {
                refundInfo.setCallbackTime(new Date());
                refundInfo.setTradeNo(resultMap.get("refund_id"));
                refundInfo.setRefundStatus(RefundStatusEnum.REFUND.getStatus());
                refundInfo.setCallbackContent(JSONObject.toJSONString(resultMap));
                refundInfoService.updateById(refundInfo); // 修改退款表中内容 退款成功
                return true;
            }
            return false;
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }
}
